Repository navigation
remote_files: Add support for directly using local cache - #87
Conversation
The Snakemake storage usually checks the existence and revalidates remote files before using the local cache, which can lead to errors when running the workflow offline even if the local cache already exists. This change adds support to directly use the local cache if it exists and bypass the remote file check when 1. the config parameter `use_remote_files` is False 2. the environment variable `NEXTSTRAIN_REMOTE_FILES` is False 3. the workflow is running offline Based on discussions in <nextstrain/public#47> and changes tested in measles repo <nextstrain/measles@60634e2...b238682>
jameshadfield
left a comment
There was a problem hiding this comment.
Thanks for taking this on and turning the prototype into a polished PR! I haven't tested the code but reading through it I think the behaviour is spot on. One minor query re: env variables is the only thing that stuck out to me.
| def is_online(host="8.8.8.8", port=53, timeout=3) -> bool: | ||
| """ | ||
| Check if workflow has network connection. | ||
| Based on <https://stackoverflow.com/a/33117579> | ||
| Host: 8.8.8.8 (google-public-dns-a.google.com) | ||
| OpenPort: 53/tcp | ||
| Service: domain (DNS/TCP) | ||
| """ |
There was a problem hiding this comment.
Cool! I like this general "is online" approach better than the per-source checks in my original prototype
There was a problem hiding this comment.
On the other hand, by checking against Google, is_online could return True when sources are unavailable, or False when sources are available but Google is down. This is very edge case-y though, I think it's fine to keep as-is.
Based on discussion in review <#87 (comment)>
|
Will merge this next Tuesday to monitor downstream uses. |
| if not local.exists(): | ||
| raise Exception( | ||
| f"Unable to find local cached file for {so.query!r}. " | ||
| "Please make sure you have run the workflow at least once with " | ||
| "internet connection and config.use_remote_files set to True to " | ||
| "to fetch and save the remote file locally.") |
There was a problem hiding this comment.
Ah, I should have tested more with wildcards. This leads to an error when the inputs are defined with wildcards, e.g. rsv.
The local path .snakemake/storage/http/data.nextstrain.org/files/workflows/rsv/{a_or_b}/metadata.tsv.gz does not exist which leads to this exception:
Exception in file "/nextstrain/build/shared/vendored/snakemake/remote_files.smk", line 233:
Unable to find local cached file for 'https://data.nextstrain.org/files/workflows/rsv/{a_or_b}/metadata.tsv.gz'. Please make sure you have run the workflow at least once with internet connection and config.use_remote_files set to True to to fetch and save the remote file locally.
File "/nextstrain/build/workflow/snakemake_rules/merge_inputs.smk", line 55, in <module>
File "/nextstrain/build/workflow/snakemake_rules/merge_inputs.smk", line 53, in _gather_inputs
File "/nextstrain/build/workflow/snakemake_rules/merge_inputs.smk", line 53, in <dictcomp>
File "/nextstrain/build/workflow/snakemake_rules/merge_inputs.smk", line 53, in <dictcomp>
File "/nextstrain/build/shared/vendored/snakemake/remote_files.smk", line 185, in path_or_url
File "/nextstrain/build/shared/vendored/snakemake/remote_files.smk", line 233, in _local_or_storage
There was a problem hiding this comment.
We've relied on the Snakemake inputs to automatically expand wildcards so path_or_url does not expand the wildcards. If we continue with that pattern and just remove this custom error, then the user will see the warning logs and the usual MissingInputsException
Using the cached copy of https://data.nextstrain.org/files/workflows/rsv/{a_or_b}/metadata.tsv.gz at .snakemake/storage/http/data.nextstrain.org/files/workflows/rsv/{a_or_b}/metadata.tsv.gz without revalidation. Set config.use_remote_files to True if you want to force revalidation.
Using the cached copy of https://data.nextstrain.org/files/workflows/rsv/{a_or_b}/sequences.fasta.xz at .snakemake/storage/http/data.nextstrain.org/files/workflows/rsv/{a_or_b}/sequences.fasta.xz without revalidation. Set config.use_remote_files to True if you want to force revalidation.
Using the cached copy of https://data.nextstrain.org/files/workflows/rsv/{a_or_b}/metadata_restricted.tsv.gz at .snakemake/storage/http/data.nextstrain.org/files/workflows/rsv/{a_or_b}/metadata_restricted.tsv.gz without revalidation. Set config.use_remote_files to True if you want to force revalidation.
Using the cached copy of https://data.nextstrain.org/files/workflows/rsv/{a_or_b}/sequences_restricted.fasta.xz at .snakemake/storage/http/data.nextstrain.org/files/workflows/rsv/{a_or_b}/sequences_restricted.fasta.xz without revalidation. Set config.use_remote_files to True if you want to force revalidation.
Assuming unrestricted shared filesystem usage.
host: 7cd889771091
Building DAG of jobs...
MissingInputException in rule merge_metadata in file "/nextstrain/build/workflow/snakemake_rules/merge_inputs.smk", line 85:
Missing input files for rule merge_metadata:
output: results/a/metadata.tsv
wildcards: a_or_b=a
affected files:
.snakemake/storage/http/data.nextstrain.org/files/workflows/rsv/a/metadata.tsv.gz
.snakemake/storage/http/data.nextstrain.org/files/workflows/rsv/a/metadata_restricted.tsv.gz
Description of proposed changes
The Snakemake storage usually checks the existence and revalidates remote files before using the local cache, which can lead to errors when running the workflow offline even if the local cache already exists.
This change adds support to directly use the local cache if it exists and bypass the remote file check when
use_remote_filesis FalseNEXTSTRAIN_REMOTE_FILESis FalseBased on discussions in nextstrain/public#47 and changes tested in measles repo
nextstrain/measles@60634e2...b238682
Related issue(s)
Part of nextstrain/public#47
Checklist
If adding a script, add an entry for it in the README.